<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app"></div>

    <script src="../lib/vue-3.js"></script>
    <script>
      const app = Vue.createApp({
        template: `
          <div>
            <button @click="visible = !visible">toggle</button>
            <hr />
            <hello v-if="visible"></hello> 
            <hr />
            <div style="height: 1000px"></div>
          </div>
        `,
        data() {
          return {
            visible: true,
          };
        },
      });

      app.component("hello", {
        template: `
          <div class="hello">Hello - {{ count }}</div>
        `,
        data() {
          return {
            count: 60,
          };
        },
        methods: {
          onScroll() {
            console.log("scroll");
          },
        },
        mounted() {
          // 监听全局的滚动条事件
          window.addEventListener("scroll", this.onScroll);

          const that = this;
          // 做一个计时器
          this.timer = setInterval(function () {
            console.log("计时器", this);
            that.count--;
          }, 1000);
        },
        unmounted() {
          console.log("unmounted");
          // 做收尾工作
          window.removeEventListener("scroll", this.onScroll);

          clearInterval(this.timer);
        },
      });

      app.mount("#app");
    </script>
  </body>
</html>
